
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.datagather.tencent;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.tompai.datagather.model.CrawlDatum;
import com.tompai.datagather.model.CrawlDatums;
import com.tompai.datagather.model.Page;
import com.tompai.datagather.plugin.berkeley.BreadthCrawler;

/**
* @desc: datagather-tools
* @name: TenArticalCrawler.java
* @author: tompai
* @createTime: 2018年5月1日 下午10:14:50
* @history:
* @version: v1.0
*/

public class TenArticalCrawler extends BreadthCrawler{

	private static final Logger logger = Logger.getLogger(TenNewsCrawler.class);
	
	private String content;
	private Integer commentNumber;
	private String author;
	private String responsEditor;
	
	public TenArticalCrawler(String crawlPath, boolean autoParse) {
		super(crawlPath, autoParse);
		//TODO Auto-generated constructor stub
	}
	
	/**
	* @author: tompai
	* @createTime: 2018年5月1日 下午10:14:50
	* @history:
	* @param args void
	 * @throws Exception 
	*/

	public static void main(String[] args) throws Exception {
		//TODO Auto-generated method stub

		String tempUrl="https://news.qq.com/a/20180430/018310.htm";
		TenArticalCrawler crawler = new TenArticalCrawler(".temp/depth_crawler", true);
	    //crawler.getConf().setTopN(5);
	    
		crawler.addSeed(new CrawlDatum(tempUrl)
                .meta("depth", 1));
		crawler.start(1);
	}
	
	@Override
	public void visit(Page page, CrawlDatums next) {
		//TODO Auto-generated method stub
		 logger.info("visiting:" + page.url() + "\tdepth=" + page.meta("depth"));
		 String html=page.html();
		 Document doc = Jsoup.parse(html);
		 String text=doc.select("#Cnt-Main-Article-QQ").first().text();
		 //logger.info("content:"+text);
		 setContent(text);
		 //================================
		 String author=doc.getElementsByClass("a_author").text();
		 //logger.info("Author:"+author);
		 setAuthor(author);
		 //================================
		 String editor=doc.getElementById("QQeditor").text();
		 //logger.info("Editor:"+editor);
		 setResponsEditor(editor);
	}
	
	public String getContent() {
	
		return content;
	}
	
	public void setContent(String content) {
	
		this.content = content;
	}

	public Integer getCommentNumber() {
	
		return commentNumber;
	}

	public void setCommentNumber(Integer commentNumber) {
	
		this.commentNumber = commentNumber;
	}

	public String getAuthor() {
	
		return author;
	}
	
	public void setAuthor(String author) {
	
		this.author = author;
	}

	public String getResponsEditor() {
	
		return responsEditor;
	}

	
	public void setResponsEditor(String responsEditor) {
	
		this.responsEditor = responsEditor;
	}
}

